package inc.gome.dynamic.datasource.oss;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

public class Os3TokenCreate {

    /**
     *       String akey = "";
     *         String skey = " ";
     *         String endpoint = "";
     * @param args
     * @throws Exception
     */
            public static void main(String[] args) throws Exception {
            //http建立连接时候先发tcp连接请求，http 1.0协议每次发送http请求都需要建立一个新的TCP连接。http1.1允许一个tcp连接反复发送和响应。http2.0允许同时发送多个http请求。不需要按序返回
            //GET请求的参数必须附加在URL上,因为URL的长度限制，GET请求的参数不能太多，而POST请求的参数就没有长度限制，因为POST请求的参数必须放到Body中。

            //生成RFC1123 格式的时间
            SimpleDateFormat sdf3 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
            sdf3.setTimeZone(TimeZone.getTimeZone("GMT"));
            String rfc1123_3 = sdf3.format(new Date());

            //签名构造参数 变为UTF-8格式
            String stringToSign ="GET\n\n\n"+rfc1123_3+"\n/demoapp";
            byte[] utf8Tosign = stringToSign.getBytes("UTF-8");
            String utf8str = new String(utf8Tosign,"UTF-8");

            //计算签名
            String s3Key="4SPIVUVBZ5E3UHM3J05S";
            byte[] s3Secret="204cvxspRKLMdPeFcKsAF5Px8Wnn5tfVYeMcHoKR".getBytes();

            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(new SecretKeySpec(s3Secret, "HmacSHA1"));
            mac.update(utf8str.getBytes());
            byte[] result = mac.doFinal();
            String signature = Base64.getEncoder().encodeToString(result);
            System.out.println("签名为： "+signature);


            URL url = new URL("http://bjgc11.gos.gomedc.com/demoapp");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setUseCaches(false);
            // conn.setConnectTimeout(30000); //30秒连接超时
            // conn.setReadTimeout(30000); //30秒读取超时
            conn.setDoOutput(true); // 设置是否向httpUrlConnection输出，默认情况下是false
            conn.setDoInput(true); //设置是否从httpUrlConnection读入，默认情况下是true;
            conn.setRequestProperty("Date", rfc1123_3);
                System.out.println(rfc1123_3);
            String headerAu="AWS" + " " + s3Key + ":" + signature;
                System.out.println(headerAu);
            conn.setRequestProperty("Authorization",headerAu );
            conn.connect();

            System.out.println(conn.getResponseCode());
            Map<String, List<String>> map = conn.getHeaderFields();
            for (String key:map.keySet()) {
           // System.out.println(key + ": " + map.get(key));
            }


            InputStream input = conn.getInputStream();
            BufferedReader buff = new BufferedReader(new InputStreamReader(input));
            String line = null;
            while((line = buff.readLine()) != null){
            line = new String(line.getBytes("UTF-8"));
            //System.out.println(line);
            }
            input.close(); //关闭流，完成后要及时关闭避免占用资源
            buff.close();
            conn.disconnect();


}

}